<!-- 旅游城市管理 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="省份名称">
                            <a-input v-model="province" allow-clear placeholder="请输入省份名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="城市名称">
                            <a-input v-model="city" allow-clear placeholder="请输入城市名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type="primary" style="margin-right: 20px" @click="getData()">查询</a-button>
                            <a-button type="" icon="plus" @click="add()">添加</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size,}" size="small">
            <!-- <div slot="city" slot-scope="city">
                <a-tag :color="colors[0]">{{city.split('-')[0]}}</a-tag>
                <a-tag :color="colors[1]">{{city.split('-')[1]}}</a-tag>
            </div> -->
            <div slot="operation" slot-scope="text,record">
                <a-button type="link" @click="update(record)">修改</a-button>
                <a-popconfirm
                    title="是否确认删除当前配置?"
                    ok-text="Yes"
                    cancel-text="No"
                    @confirm="del(record)"
                >
                    <a-button class="errorColor" type="link">删除</a-button>
                </a-popconfirm>
            </div>
        </a-table>

        <a-modal
            title="添加、修改旅游城市配置"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="城市名称">
                        <a-cascader v-model="defaultValue" :options="options" :fieldNames="{label: 'areaName','value': 'areaName',children: 'children'}"  :load-data="loadData" change-on-select placeholder="请选择旅游城市" />
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryTravelCity , updateTravelCity } from '@/api/travel';
import { queryAreaInfo } from '@/api/brand';
import { columns } from './table';
export default {
    data () {
        return {
            province: '',
            city: '',
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            columns,
            item: {
                'id': '',
                'title': '', // 标题
                'itemType': undefined, // 项目类型
                'amount': '', // 价格
                'titleImgUrl': '', // 标题图片
            },
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            confirmLoading: false,
            isAdd: false,
            visible: false,
            options: [], // 地区 筛选
            defaultValue: [],
        };
    },
    async created(){
        try {
            this.provinceList = await this._queryAreaInfo('0');
            this.options = this.provinceList;
            this.getData();
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 查询城市配置
        getData(){
            let _this = this;
            _this.isload = true
            const params = {
                'province': _this.province,
                'city': _this.city,
                'page': _this.page - 1,
                'size': _this.size,
            }
            queryTravelCity(params)
            .then(res => {
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data
                    _this.total = res.data.length
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
        },
        // 添加产品
        add(){
            this.item.id = ''
            this.item.city = '';
            this.defaultValue = [];
            this.visible = true
        },
        // 修改产品
        async update(item){
            let _this = this;
            this.item = JSON.parse(JSON.stringify(item))
            _this.defaultValue = item.city.split('-');
            var province = _this.options.find(itm => itm.areaName == item.province);
            var selOption = [province];
            await _this.loadData(selOption);
            this.visible = true
        },
        // 确认添加、修改等级产品
        async handleOk(){ 
            let _this = this
            if(_this.defaultValue.length != 2){
                _this.$message.warning('请选择地区')
                return;
            }
            const params = {
                'id':_this.item.id,
                'province': _this.defaultValue[0],
                'city': _this.defaultValue[1],
            }
            updateTravelCity(params)
            .then(async res=>{
                if(res.success){
                    _this.visible = false
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.id ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除产品
        del(item){
            let _this = this
            const params = {
                'id':item.id,
                'province': item.province,
                'city': item.city,
                'isDelete':'1',
            }
            updateTravelCity(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 获取省市区
        async _queryAreaInfo(areaParentId){
            return new Promise((resolve,reject) => {
                queryAreaInfo(areaParentId)
                .then(res => {
                    if(res.success){
                        var list = [];
                        for (let index = 0; index < res.data.length; index++) {
                            const element = res.data[index];
                            if(areaParentId == '0'){
                                element.isLeaf = false;
                            }
                            list.push(element);
                        }
                        resolve(list)
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        async loadData(selectedOptions) {
            const targetOption = selectedOptions[selectedOptions.length - 1];
            targetOption.loading = true;
            let list = await this._queryAreaInfo(selectedOptions[selectedOptions.length - 1].areaId);
            targetOption.loading = false;
            targetOption.children = list;
            this.options = [...this.options];
        },
    }
}

</script>
<style scoped>

</style>